package GUILayer;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

import CtrLayer.SupplierCtr;
import ModelLayer.Supplier;

public class SupplierTableModel  extends AbstractTableModel {
	private ArrayList<Supplier> li = new ArrayList<Supplier>();
	private String[] columnNames = { "ID", "Name", "Address",
			"Country", "Phone", "Email"};
	private SupplierCtr supCtr;

	public SupplierTableModel(){
		supCtr = new SupplierCtr();
	}
	
	public void populateList()
	{
		ArrayList<Supplier>tmpList = supCtr.listSuppliers();
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}
	public void populateList(String filter)
	{
		ArrayList<Supplier>tmpList = supCtr.listSuppliers(filter);
		if(tmpList != null)
		{
			this.li.clear();
			this.li = tmpList;
		}
		fireTableDataChanged();
	}

	@Override
	public String getColumnName(int columnIndex){
		return columnNames[columnIndex];
	}

	@Override     
	public int getRowCount() {
		return li.size();
	}

	@Override        
	public int getColumnCount() {
		return 6; 
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Supplier s = li.get(rowIndex);
		switch (columnIndex) {
		case 0: 
			return s.getId();
		case 1:
			return s.getName();
		case 2:
			return s.getAddress();
		case 3:
			return s.getCountry();
		case 4:
			return s.getPhoneNo();
		case 5:
			return s.getEmail(); 
		}
		return null;
	}

	@Override
	public Class<?> getColumnClass(int columnIndex){
		switch (columnIndex){
		case 0:
			return Integer.class;
		case 1:
			return String.class;
		case 2:
			return String.class;
		case 3:
			return String.class;
		case 4:
			return String.class;
		case 5:
			return String.class;
		}
		return null;
	}
}